<?php
if($_REQUEST['_m'] == "del") {
	
	$q = mysql_fetch_array(mysql_query("select cover_image  from books where id='".$_REQUEST['book_id']."'"));
	$path = "../uploads/books/".$q["cover_image"];
	@unlink($path);
	
	$sql = "delete from books where id='".$_REQUEST['book_id']."'";
	mysql_query($sql) or die(mysql_error());
}
?>
<script type="text/javascript">
function validateBook()
{
	
	var book_title = document.formBook.book_title.value;
	var book_author = document.formBook.book_author.value;
	var publisher = document.formBook.publisher.value;
	var edition = document.formBook.edition.value;
	var published_in = document.formBook.published_in.value;
	var isbn = document.formBook.isbn.value;
	
	document.formBook.method = "post";
	document.formBook.action = "action_book.php";
	document.formBook.submit;
}
</script>
<table cellspacing="0" cellpadding="0" border="0" align="center" width="100%">
<tr>
<td>Books</td>
<td align="right"><a href="index.php?tc=books_add" class="font12">Add New</a></td>
</tr>
<tr>
<td colspan="100%">
<form name="formBook" onsubmit="return validateBook()">
<table cellspacing="0" cellpadding="0" border="1" align="center" width="100%"  class="list">
  <tbody>
  <tr style="background-color:#CCC">
   <td>#</td>
   <td>Book Title</td>
   <td>Book Desc</td>
   <td>Author</td>
   <td>Publisher</td>
   <td>Edition</td>
   <td>Published In</td>
   <td>ISBN No.</td>
   <td>Price</td>
   <td>Cover Image</td>
   <td>Status</td>
   <td>Action</td>
  </tr>
  <?php
  
include('includes/function.php');
$self="index.php?tc=books&_st=".$_REQUEST['_st'];
$rowsPerPage = 10;
$pageNum = 1;
if(isset($_GET['page']))
{
	$pageNum = $_GET['page'];
}
if($_GET['page']=="")
{
	$pageNum = 1;
}


$offset = ($pageNum - 1) * $rowsPerPage;
$st = $_REQUEST['_st'];

if($st == "" || $st == 2)
{
	$res = mysql_query("select id from books");
	
	$query = mysql_query("select id,`book_name`, `book_desc`, `author`, `ISBN`, `publisher`, `edition`, `store_id`, `published_in`, `cover_image`,`price`,`active_status` from books   order by id desc limit $offset , $rowsPerPage") or die(mysql_error());
}
else
{
	if($st == 3) $st=0;
	$res = mysql_query("select id from books where active_status='$st'");
	
	$query = mysql_query("select id,`book_name`, `book_desc`, `author`, `ISBN`, `publisher`, `edition`, `store_id`, `published_in`, `cover_image`,`price`,`active_status`  from books where active_status='$st' order by id desc limit $offset , $rowsPerPage");
}

  /*$sql = "select id,`book_name`, `book_desc`, `author`, `ISBN`, `publisher`, `edition`, `store_id`, `published_in`, `cover_image`,`price`,`active_status` from books order by id desc";
  $res = mysql_query($sql) or die(mysql_error()); */
  $i = 1;
  while($data = mysql_fetch_array($query)) {
	
	  ?>
	  <tr class="font12">
      <td align="center"><?php echo $i+$offset;?></td>
      <td>&nbsp;<?php echo $data['book_name'];?></td>
      <td>&nbsp;<?php echo stripcslashes(substr($data['book_desc'],0,30));?>...</td>
      <td>&nbsp;<?php echo $data['author'];?></td>
      <td>&nbsp;<?php echo $data['publisher'];?></td>
      <td>&nbsp;<?php echo $data['edition'];?></td>
      <td>&nbsp;<?php echo $data['published_in'];?></td>
      <td>&nbsp;<?php echo $data['ISBN'];?></td>
       <td>&nbsp;<?php echo $data['price'];?></td>
       <?php 
		if($data["cover_image"]=="")
		{
		$source ='<img src="thumb.php?file=images/noimage.jpg" width="80px" />';	
		}
		else
		{
		$source ='<img width=80px  src="thumb.php?file=../uploads/books/'.$data['cover_image'].'"/>';
		}
		 
		?>
        <td align="center"><?php echo $source;?></td>	
      <td>&nbsp;<?php  
	  if($data['active_status'] == "1")
	  {
		  echo "Activated";
	  }
	  else {
		   echo "Deactivated";
	  }
	  ?></td>
      <td><a href="index.php?tc=books_add&book_id=<?php echo $data['id'];?>" class="font12">Edit</a>/<a href="index.php?tc=books&book_id=<?php echo $data['id'];?>&_m=del" class="font12" onClick="return confirm('Are you sure want to delete?');">Delete</a></td>
      </tr>
  <?php    
    $i++;}
  ?>
</tbody></table>
</form>
</td>
</tr>
<?php 
if(mysql_num_rows($res) <= $rowsPerPage){	}
else { ?>
<tr>
    <td colspan="13" align="center"  bgcolor="#FFFFFF">
        <?php echo $paginglink=getPagingLink($res, $rowsPerPage,$pageNum,$self); ?>	
    </td>
</tr>
<?php } ?>
</table>